package cn.regexp.business.web.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.regexp.common.base.page.TablePageInfo;
import cn.regexp.common.web.AjaxResult;
import cn.regexp.domain.ShopGoods;
import cn.regexp.domain.ShopGoodsAttr;
import cn.regexp.query.GoodsQuery;
import cn.regexp.service.IGoodsAttrService;
import cn.regexp.service.IGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;

/**
 * 商品属性控制器
 */
@Controller
@RequestMapping("business/attr")
public class GoodsAttrController {
    //模板前缀
    private static final String prefix = "business/goods/attr/";

    @Autowired
    private IGoodsAttrService goodsAttrService;
    @Autowired
    private IGoodsService goodsService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:attr:view")
    @RequestMapping("/listPage")
    public String listPage(Model model, Long goodsId) {
        model.addAttribute("goods", ObjectUtil.isNotEmpty(goodsId) ? goodsService.getById(goodsId) : new ShopGoods());
        return prefix + "list";
    }

    @RequiresPermissions("business:attr:add")
    @RequestMapping("/addPage")
    public String addPage(Model model, Long goodsId) {
        model.addAttribute("goods", ObjectUtil.isNotEmpty(goodsId) ? goodsService.getById(goodsId) : new ShopGoods());
        return prefix + "add";
    }

    @RequiresPermissions("business:attr:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("attr", goodsAttrService.getById(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:attr:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ShopGoodsAttr> query(GoodsQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        QueryWrapper<ShopGoodsAttr> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(qo.getId()), "goods_id", qo.getId())
                .eq(ObjectUtil.isNotEmpty(qo.getKeyword()), "goods_name", qo.getKeyword());
        return new TablePageInfo<>(goodsAttrService.list(wrapper), goodsAttrService.count(wrapper));
    }


    //新增
    @RequiresPermissions("business:attr:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShopGoodsAttr attr) {
        goodsAttrService.save(attr);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:attr:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ShopGoodsAttr attr) {
        goodsAttrService.updateById(attr);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:attr:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        goodsAttrService.removeByIds(Collections.singletonList(ids));
        return AjaxResult.success();
    }
}
